library(Seurat)
library(monocle)
data<-readRDS("T_seurat_data.Rds")
t.group<-read.csv('t_clutser.csv',quote='',header=T,row.names=1,check.names=F)
data$nmf_cluster<-t.group$nmf_cluster
cell<-data@meta.data
data<-data@assays$RNA@counts
data <- CreateSeuratObject(counts = data)
data<-SplitObject(data, split.by = "orig.ident")
for (i in data){
    i <- NormalizeData(i, normalization.method = "LogNormalize")
    i <- FindVariableFeatures(i, nfeatures = 2000)
}
AllBatch.anchors <- FindIntegrationAnchors(object.list = data, dims = 1:30)
data<- IntegrateData(anchorset = AllBatch.anchors, dims = 1:30,k.weight=50)
data <- ScaleData(data, verbose = FALSE,features =rownames(data))
data <- RunPCA(data, features = VariableFeatures(object = data),npcs=50)
data <- RunTSNE(data, dims = 1:30)
data@meta.data<-cell
data$label<-data$celltype_minor
pdf('DimPlot_t_clutser.pdf',width=5,height=4)
DimPlot(data, group.by = 'label', reduction='tsne')
dev.off()

cell<-data@meta.data
library(ggplot2)
library(RColorBrewer)
pdf('cluster_NMF.pdf',width=5,height=5)
ggplot(cell,aes(x=nmf_cluster,y=1,fill=label))+geom_bar(stat="identity",position="fill")+labs(x='',y='Cell Proportion')+theme_bw()+theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+theme(panel.border=element_blank())+theme(axis.line=element_line(colour="black"))
dev.off()

data@active.ident<-factor(data$nmf_cluster)
expr_matrix <- as(as.matrix(data@assays$RNA@counts),'sparseMatrix')
pdata <- data@meta.data
data@active.assay<-'RNA'
fdata <- data.frame(gene_short_name = row.names(data),row.names = row.names(data))
pd <- new('AnnotatedDataFrame', data = pdata)
fd <- new('AnnotatedDataFrame', data = fdata)
cds <- newCellDataSet(expr_matrix,phenoData = pd,featureData = fd,expressionFamily = negbinomial.size(),lowerDetectionLimit=0.1)
cds <- estimateSizeFactors(cds)
cds <- estimateDispersions(cds)
data <- FindVariableFeatures(data, nfeatures = 2000)
gene <- VariableFeatures(data)
cds <- setOrderingFilter(cds,gene)
cds <- reduceDimension(cds, max_components = 2,method = "DDRTree",norm_method='log')
data<-NormalizeData(data, normalization.method = "LogNormalize")
scale<-as.data.frame(data@assays[["RNA"]]@data)
scale<-scale[rowSums(scale)>0,]
scale<-as.data.frame(scale(scale))
scale$id<-rownames(scale)
sig<-read.csv('pcbc-stemsig.csv',quote='',header=T,row.names=1,check.names=F)
scale<-merge(sig,scale,by='id')
rownames(scale)<-scale$id
scale<-scale[,-1]
cor<-as.data.frame(t(cor(scale[,1],scale[,-1],method = 'spearman',use="complete.obs")))
names(cor)<-'mRNAsi'
cor$mRNAsi<-cor$mRNAsi-min(cor$mRNAsi)
cor$mRNAsi<-cor$mRNAsi/max(cor$mRNAsi)
cds <- orderCells(cds)
cor<-cbind(cor,pData(cds))
library(ggpubr)
library(ggsignif)
library(ggforce)
library(ggplot2)
cor$State<-paste('stahe',cor$State,sep='')
ggplot(cor,aes(x=State,y=mRNAsi))+geom_boxplot(width=0.5,aes(fill=State))+ theme(axis.text.x = element_text(angle = 45, hjust = 1))+theme_classic()
ggsave('stage_mRNAsi.pdf',width=6,height=5)

cds <- orderCells(cds,root_state = 1)
plot_cell_trajectory(cds, color_by="Pseudotime") 
ggsave('tree_Pseudotime.pdf',width=7,height=7)
plot_cell_trajectory(cds, color_by="nmf_cluster")
ggsave('tree_nmf.pdf',width=7,height=7)
plot_cell_trajectory(cds, color_by="label")
ggsave('tree_cluster.pdf',width=7,height=7)
Time_diff <- differentialGeneTest(cds[gene,],cores = 10)
Time_diff<-Time_diff[Time_diff$qval<0.05,]
pdf("Time_heatmapAll.pdf",width = 5,height = 10)
plot_pseudotime_heatmap(cds[Time_diff$gene_short_name,],norm_method='log',return_heatmap=T)
dev.off()

a<-data@assays$RNA@data
a<-as.data.frame(t(a))
gene<-read.csv('ferroptosis.csv',quote='',header = T,check.names=F)
gene<-a[,names(a)%in%gene$id]
gene$cluster<-pdata$nmf_cluster
gene<-aggregate(gene[,-395],by=list(gene$cluster),mean)
rownames(gene)<-gene$Group.1
gene<-gene[,-1]
gene<-as.data.frame(t(gene))
gene<-gene[order(rowSums(gene),decreasing = T),]
gene<-gene[1:50,]
number<-as.data.frame(table(pdata$nmf_cluster))

library('ComplexHeatmap')
library('circlize')
bar <- HeatmapAnnotation(sum = anno_barplot(number$Freq,bar_width = 0.9,gp = gpar(col = "white", fill = "grey"),border = F,axis_param = list(at = c(0,3000,6000),labels = c("0","3000","6000")),height = unit(2, "cm")), show_annotation_name = F)
pdf('heatmap_T_nmf.pdf',width=8,height=8)
Heatmap(t(scale(t(gene))),top_annotation=bar,cluster_columns = FALSE,name = "AveExpression",column_names_rot=0)
dev.off()

t_gene<-read.csv('T_related_genes.csv',quote='',header=T,check.names=F)
a<-a[,names(a)%in%t_gene$gene]
a<-as.data.frame(t(a))
a$gene<-rownames(a)
a<-merge(t_gene,a,by='gene')
a<-a[order(a$terms),]
t_gene<-a[,1:2]
a<-as.data.frame(t(a[,-1:-2]))
names(a)<-t_gene$gene
data$nmf_cluster<-paste(data$label,data$nmf_cluster,sep='')
cell<-data@meta.data
a<-cbind(cell,a)
a<-a[,-1:-10]
a<-aggregate(a[,-1],by=list(a$nmf_cluster),mean)
rownames(a)<-a$Group.1
a<-a[,-1]
library(pheatmap)
annotation_row=as.data.frame(t_gene$terms)
names(annotation_row)<-'terms'
rownames(annotation_row)<-names(a)
annotation_col=c(rep('Cycling T-cells',3),rep('NK cells',3),rep('NKT cells',3),rep('T cells CD4+',3),rep('T cells CD8+',3))
annotation_col<-as.data.frame(annotation_col)
rownames(annotation_col)<-rownames(a)
names(annotation_col)<-'Type'
pheatmap(t(a), color=colorRampPalette(c("blue", "white", "red"))(100),scale='row',cluster_rows=FALSE,show_rownames=FALSE,cluster_cols=FALSE,annotation_col = annotation_col,annotation_row = annotation_row,annotation_colors=list(`Type`=c('Cycling T-cells'="red",'NK cells'="blue",'NKT cells'='green','T cells CD4+'='gold','T cells CD8+'='pink')),gaps_row = c(33,63,69,74,94,103,111),gaps_col = c(3,6,9,12),filename='heatmap_T_sig.pdf',height=6,width=5)

library(iTALK)
library(Matrix)
library(dplyr)
library(RColorBrewer)
all<- readRDS("seurat_annotated_data.Rds")
all<-SplitObject(all, split.by = "label")
all<-all[c(5,12)]
all[['T-cells']]<-data
all<-merge(all[[1]],all[[2]])
all$nmf_cluster<-ifelse(is.na(all$nmf_cluster),all$label,all$nmf_cluster)
iTalk_data <- as.data.frame(t(all@assays$RNA@counts))
iTalk_data$cell_type <- all$nmf_cluster
iTalk_data$compare_group <- 'Group'
highly_exprs_genes <- rawParse(iTalk_data, top_genes=50, stats="mean")
comm_list<-c('growth factor','other','cytokine','checkpoint')
cell_types <- unique(iTalk_data$cell_type)
iTalk_res <- NULL
for(comm_type in comm_list){
  res_cat <- FindLR(highly_exprs_genes, datatype='mean count', comm_type=comm_type)
  iTalk_res <- rbind(iTalk_res, res_cat)
}
iTalk_res <- iTalk_res[order(iTalk_res$cell_from_mean_exprs*iTalk_res$cell_to_mean_exprs,decreasing=T),]
iTalk_res<-iTalk_res[iTalk_res$cell_from=='Cancer Epithelial'| iTalk_res$cell_to=='Cancer Epithelial',]
mycolors <- c(brewer.pal(12,'Set3'),brewer.pal(9,'Set2'))
cell_col <- structure(mycolors[1:length(cell_types)], names=cell_types)
pdf('NetView.pdf',width=10,height=10)
NetView(iTalk_res,col=cell_col,vertex.size=18,vertex.label.cex=1,arrow.width=1,edge.max.width=10,label = FALSE)
dev.off()
saveRDS(data, file="T_seurat_data_NMF.Rds")

data@active.ident<-factor(data$nmf_cluster)
data.markers <- FindAllMarkers(data,logfc.threshold = 0.15,only.pos =TRUE)
data.markers<-data.markers[data.markers$p_val_adj<0.05,]
library(dplyr)
top <- data.markers %>% group_by(cluster) %>% top_n(n = 100, wt = avg_log2FC)
top<-top[!duplicated(top$gene),]$gene
write.csv(data.markers,'T_makers_pos.csv',quote=F)
rm(iTalk_data,number,highly_exprs_genes,bar,comm_list,cell_types,cell_col,mycolors,res_cat,comm_type,pdata,data,all,AllBatch.anchors,i,cds,fd,pd,fdata,scale,sig,expr_matrix,Time_diff)